DataTable服务器端处理如果没有数据被收回,如何禁用自定义

时间:2017-01-26 16:34:33

标签: jquery datatables datatables-1.10

我使用过dataTable服务器端处理我想在没有收到数据的情况下禁用按钮。

<?xml version="1.0" encoding="utf-8"?>

<project name="MY-PROJECT" default="create-war-and-deploy-to-jboss" basedir="." xmlns:sonar="antlib:org.sonar.ant" xmlns:jacoco="antlib:org.jacoco.ant">

    <property name="verbose" value="false" />
    <property name="javaVersion" value="1.7" />

    <!-- Provides access to OS environment variables (eg: env.USERNAME) -->
    <property environment="env" />

    <!-- Application's directories -->
    <property name="dir.build" value="${basedir}/build" />
    <property name="dir.src" value="${basedir}/src" />
    <property name="dir.dist" value="${basedir}/dist" />
    <property name="dir.lib" location="${basedir}/lib" />
    <property name="dir.target" value="${basedir}/target" />
    <property name="dir.build.classes" value="${dir.target}/classes" />
    <property name="dir.test" value="${basedir}/test" />
    <property name="dir.web" value="${basedir}/WebContent" />
    <property name="dir.webinf.lib" value="${dir.web}/WEB-INF/lib" />

    <!-- Loading the application.properties file -->
    <property file="${dir.src}/resources/application.properties" prefix="app." />

    <!-- Loading the build.properties file -->
    <property file="build.properties" prefix="buildProp." />

    <tstamp>
        <format property="build.time" pattern="yyyy-MM-dd HH:mm" />
    </tstamp>

    <echo message="========================================================================" />
    <echo message="*** Starting ${app.name} build " />
    <echo message="========================================================================" />

    <!-- JBoss directories -->
    <property name="dir.jboss" value="${buildProp.jboss.home}" />
    <property name="dir.jboss.domain" value="${dir.jboss}/${buildProp.jboss.domain}" />
    <property name="dir.jboss.libs" value="${dir.jboss}/${buildProp.jboss.libs}" />
    <property name="dir.jboss.deploy" value="${dir.jboss.domain}/${buildProp.jboss.deploy}" />

    <!-- Path for unit tests -->
    <path id="run.classpath.tests">
        <pathelement path="${dir.build.classes}" />
        <pathelement path="${dir.test}/build/classes" />
        <path refid="compile.classpath.tests" />
    </path>

    <!-- Provided dependencies from the container -->
    <path id="compile.classpath.server">
        <fileset dir="${dir.lib}">
            <include name="**/*.jar" />
        </fileset>
    </path>

    <!-- Dependencies specific to this application -->
    <path id="compile.classpath.web">
        <fileset dir="${dir.web}/WEB-INF/lib">
            <include name="**/*.jar" />
        </fileset>
    </path>

    <path id="compile.classpath.target">
        <fileset dir="${dir.lib}">
            <include name="hibernate-jpamodelgen-5.1.0.Final.jar" />
        </fileset>
        <fileset dir="${dir.target}">
            <include name="**/*.java" />
        </fileset>
    </path>

    <!-- Dependencies specifically for unit testing purposes -->
    <path id="compile.classpath.tests">
        <fileset dir="${dir.test}/lib/">
            <include name="junit-4.6.jar" />
            <include name="mockito-all-1.9.5.jar" />
        </fileset>
        <fileset dir="${dir.webinf.lib}" />
        <pathelement location="${dir.build.classes}" />
        <path refid="compile.classpath.server" />
    </path>

    <!-- PRINT-VERSION -->
    <target name="print-version"> 
       <echo>Java/JVM version: ${ant.java.version}</echo> 
       <echo>Java/JVM detailed version: ${java.version}</echo> 
    </target>

    <!-- BUILD-DISTRIBUTION -->
    <target 
        name="build-distribution" 
        description="Generate Deployment unit and stage to appserver directory." 
        depends="
            clean,
            run-unit-tests,
            create-war,
            run-sonarqube-analysis">

        <manifest file="${dir.web}/META-INF/MANIFEST.MF">
            <attribute name="Manifest-Version" value="1.0" />
            <attribute name="Ant-Version" value="1.7.0" />
            <attribute name="Created-By" value="${env.USERNAME}" />
            <attribute name="Implementation-Title" value="${app.name}" />
            <attribute name="Implementation-Version" value="${app.version}" />
            <attribute name="Implementation-Vendor" value="Canada Revenue Agency" />
            <attribute name="Implementation-Date" value="${build.time}" />
            <attribute name="Built-By" value="${env.USERNAME}" />
            <attribute name="Dependencies" value="org.hibernate" />
        </manifest>

        <propertyfile file="${dir.src}/resources/application.properties">
            <entry key="lastUpdate" value="${build.time}" />
        </propertyfile>

    </target>

    <!-- CLEAN -->
    <target name="clean" 
        description="Cleans up build-related temporary directories." 
        depends="delete-directories" />

    <!-- CREATE-DIRECTORIES -->
    <target name="create-directories" >
        <mkdir dir="${dir.build}" />
        <mkdir dir="${dir.dist}" />
        <mkdir dir="${dir.build.classes}" />
        <mkdir dir="${dir.web}/WEB-INF/classes" />
        <mkdir dir="${dir.test}/build" />
        <mkdir dir="${dir.test}/build/classes" />
        <mkdir dir="${dir.test}/reports" />
    </target>

    <!-- DELETES-DIRECTORIES -->
    <target name="delete-directories">

        <delete dir=".sonar" />

        <!-- /build -->
        <delete includeEmptyDirs="true" failonerror="false">
            <fileset dir="${dir.build}" includes="**/*" />
            <exclude name=".cvsignore" />
        </delete>

        <!-- /dist -->
        <delete includeEmptyDirs="true" failonerror="false">
            <fileset dir="${dir.dist}" includes="**/*" />
            <exclude name=".cvsignore" />
        </delete>

        <!-- /WEB-INF/classes -->
        <delete includeEmptyDirs="true" failonerror="false">
            <fileset dir="${dir.web}/WEB-INF/classes">
                <include name="**/*" />
            </fileset>
            <fileset dir="${dir.web}/WEB-INF/src">
                <include name="**/*" />
            </fileset>
        </delete>

        <!-- /target -->
        <delete includeEmptyDirs="true" failonerror="false">
            <fileset dir="${dir.target}" includes="**/*" />
            <exclude name=".cvsignore" />
        </delete>

        <!-- /test/build -->
        <delete dir="${dir.test}/build" />

        <!-- /test/reports -->
        <delete includeEmptyDirs="true" failonerror="false">
            <fileset dir="${dir.test}/reports" />
        </delete>

        <!-- Existing WAR file and JBoss marker files -->
        <delete dir="${dir.jboss.deploy}/${app.name}.war" />
        <delete file="${dir.jboss.deploy}/${app.name}.war" />
        <delete file="${dir.jboss.deploy}/${app.name}.war.deployed" />
    </target>

    <!-- COMPILE -->
    <target name="compile" depends="print-version,create-directories">
        <javac destdir="${dir.build.classes}" verbose="${verbose}" debug="on" fork="true" failonerror="true" source="${javaVersion}" target="${javaVersion}" includeantruntime="false">
            <classpath>
                <path refid="compile.classpath.server" />
                <path refid="compile.classpath.target" />
                <path refid="compile.classpath.web" />
            </classpath>
            <src path="${dir.src}" />

            <compilerarg value="-Xlint:deprecation" />
            <compilerarg value="-Xlint:unchecked" />
        </javac>

        <!--copy property files to classes directory -->
        <copy todir="${dir.build.classes}">
            <fileset dir="${dir.src}">
                <include name="**/*.properties" />
                <include name="META-INF/**/*" />
            </fileset>
        </copy>
    </target>

    <!-- COMPILE-TESTS -->
    <target name="compile-tests" depends="create-directories,compile">
        <javac srcdir="${dir.test}/src" destdir="${dir.test}/build/classes" verbose="${verbose}" debug="on" fork="true" source="${javaVersion}" target="${javaVersion}" includeantruntime="false">
            <classpath refid="compile.classpath.tests" />
            <compilerarg value="-Xlint:deprecation" />
            <compilerarg value="-Xlint:unchecked" />
        </javac>
    </target>

    <!-- RUN-UNIT-TESTS -->
    <target name="run-unit-tests" depends="compile-tests" description="Runs all unit test classes under the test package.">

        <taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml">
            <classpath path="test/lib/org.jacoco.ant-0.7.2.201409121644.jar"/>
            <classpath path="test/lib/org.jacoco.agent_0.7.2.201409121644.jar"/>
            <classpath path="test/lib/org.jacoco.core_0.7.2.201409121644.jar"/>
            <classpath path="test/lib/org.jacoco.report_0.7.2.201409121644.jar"/>
        </taskdef>

        <jacoco:coverage destfile="${dir.target}/jacoco.exec">
            <junit fork="true" printsummary="yes" haltonfailure="yes" showoutput="no">
                <classpath refid="run.classpath.tests" />
                <formatter type="xml" />
                <batchtest fork="yes" todir="${dir.test}/reports">
                    <fileset dir="${dir.test}/src">
                        <include name="**/*Test.java" />
                    </fileset>
                </batchtest>
            </junit>
        </jacoco:coverage>
    </target>

    <!-- CREATE-WAR -->
    <target name="create-war" description="Creates a WAR file including all compiled classes and resources." depends="compile">
        <war destfile="dist/${app.name}.war" webxml="WebContent/WEB-INF/web.xml">
            <fileset dir="WebContent" />
            <classes dir="${dir.build.classes}" />
        </war>
    </target>

    <!-- CREATE-WAR-AND-DEPLOY -->
    <target name="create-war-and-deploy-to-jboss" description="Creates a WAR file including all compiled classes and resources, and deploys the app to jboss" depends="create-war,deploy-to-jboss" />

    <!-- RUN-SONARQUBE -->
    <target name="run-sonarqube-analysis" description="Runs the SonarQube analysis and updates statistics on the server." depends="create-war">

        <property file="sonar.properties" prefix="sonar." />

        <!-- SonarQube properties -->
        <property name="sonar.exclusions" value="${sonar.exclusions}" />
        <property name="sonar.host.url" value="${sonar.host.url}" />
        <property name="sonar.jacoco.reportPath" value="${sonar.jacoco.reportPath}" />
        <property name="sonar.java.binaries" value="${sonar.java.binaries}" />
        <property name="sonar.java.coveragePlugin" value="${sonar.java.coveragePlugin}" />
        <property name="sonar.java.libraries" value="${sonar.java.libraries}" />
        <property name="sonar.java.source" value="${javaVersion}" />
        <property name="sonar.junit.reportsPath" value="${sonar.junit.reportsPath}" />
        <property name="sonar.projectKey" value="${sonar.projectKey}" />
        <property name="sonar.projectVersion" value="${app.version}" />
        <property name="sonar.projectName" value="${sonar.projectName}" />
        <property name="sonar.sources" value="${sonar.sources}" />
        <property name="sonar.sourceEncoding" value="${sonar.sources.sourceEncoding}" />
        <property name="sonar.tests" value="${sonar.tests}" />

        <taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml">
            <classpath path="${dir.lib}/sonarqube-ant-task-2.4.1.jar" />
        </taskdef>

        <sonar:sonar/>
    </target>

    <!-- DEPLOY-TO-JBOSS -->
    <target name="deploy-to-jboss" depends="create-war">

        <!--delete war file if it already exists and was copied over, we need a directory now-->
        <delete file="${dir.jboss.deploy}/${app.name}.war" />
        <delete file="${dir.jboss.deploy}/${app.name}.war.deployed" />

        <!-- copy all of the files except one that will trigger the deployment -->
        <copy todir="${dir.jboss.deploy}">
            <fileset file="${dir.dist}/${app.name}.war" />
        </copy>

    </target>
</project>

以下是我的按钮

 var thisTable = $('#examole).DataTable({
        "responsive": true,
        "processing": true,
        "serverSide": true,
        "info": true,
        "order": [[8, 'desc' ]],
        "dom": '<"top"flip>rt<"bottom"flp>',
        "ajax": {
            url: '/user.php',
            type: 'POST',
        },

1 个答案:

答案 0 :(得分:0)

        var thisTable = $('#examole').DataTable({
            "responsive": true,
            "processing": true,
            "serverSide": true,
            "info": true,
            "order": [[8, 'desc' ]],
            "dom": '<"top"flip>rt<"bottom"flp>',
            "ajax": {
                url: '/user.php',
                type: 'POST',

                // add this section
                dataFilter: function (data) {
                    // you may have to deserilize and reserialize data here
                    // depending how your data is handled.
                    if(data.data.length == 0) {
                        $("#btn1").prop("disabled", true);
                    }
                    else {
                        $("#btn1").prop("disabled", false);
                    }
                    return data; 
                }
            },
            // and the rest of your code